import { http } from '@/utils/request'
import type {
  WorkOrder,
  WorkOrderAddRequest,
  WorkOrderUpdateRequest,
  WorkOrderQueryRequest,
  WorkOrderAssignRequest,
  WorkOrderReviewRequest,
  WorkOrderStatsResponse,
  UpdateRequest,
  BaseResponse,
  PageResponse
} from '@/types'

// 创建工单
export const createWorkOrder = (data: WorkOrderAddRequest): Promise<BaseResponse<number>> => {
  return http.post('/workorder/add', data)
}

// 分页获取工单列表
export const getWorkOrderList = (data: WorkOrderQueryRequest): Promise<BaseResponse<PageResponse<WorkOrder>>> => {
  return http.post('/workorder/list/page', data)
}

// 获取工单详情（别名，用于详情页面）
export const getWorkOrderDetail = (data: UpdateRequest): Promise<BaseResponse<WorkOrder>> => {
  return http.post('/workorder/get', data)
}

// 注意：工单状态更新统一使用 updateWorkOrder 接口
// 取消工单：updateWorkOrder({ id, status: WorkOrderStatus.CANCELLED })
// 处理工单：updateWorkOrder({ id, status: WorkOrderStatus.IN_PROGRESS })
// 完成工单：updateWorkOrder({ id, status: WorkOrderStatus.COMPLETED })

// 更新工单
export const updateWorkOrder = (data: WorkOrderUpdateRequest): Promise<BaseResponse<boolean>> => {
  return http.post('/workorder/update', data)
}

// 删除工单
export const deleteWorkOrder = (data: UpdateRequest): Promise<BaseResponse<boolean>> => {
  return http.post('/workorder/delete', data)
}

// 分配工单
export const assignWorkOrder = (data: WorkOrderAssignRequest): Promise<BaseResponse<boolean>> => {
  return http.post('/workorder/assign', data)
}

// 评价工单
export const reviewWorkOrder = (data: WorkOrderReviewRequest): Promise<BaseResponse<boolean>> => {
  return http.post('/workorder/review', data)
}

// 获取工单统计信息
export const getWorkOrderStats = (): Promise<BaseResponse<WorkOrderStatsResponse>> => {
  return http.post('/workorder/stats')
}
